package app.stock.client;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTextField;

import app.common.Util;
import app.stock.common.Constants;
import app.stock.common.Stock;
import app.stock.common.Transaction;
import app.stock.common.TransactionTypes;
import app.stock.server.StockManager;

public class TransactionPanel extends JDialog {
	private JPanel jPanel1;
	protected JFormattedTextField jTextFieldDate;
	protected JComboBox jComboBoxType;
	protected JComboBox jComboBoxCode;
	protected JTextField jTextFieldName;
	protected JFormattedTextField jTextFieldPrice;
	protected JFormattedTextField jTextFieldAmount;
	protected JFormattedTextField jTextFieldSum1;
	protected JFormattedTextField jTextFieldShouxufei;
	protected JFormattedTextField jTextFieldYinhuashui;
	protected JFormattedTextField jTextFieldGuohufei;
	protected JFormattedTextField jTextFieldSum2;
	protected JFormattedTextField jTextFieldCurrentCash;

	protected JButton jButtonOK;
	protected JButton jButtonCancel;
	private JPanel jPanel2;

	protected StockManager sm; // @jve:decl-index=0:
	protected List stList;
	protected List codeList;
	protected List nameList;
	private JPanel jPanel3;

	protected Transaction tx;

	public TransactionPanel(Frame owner) {
		super(owner, true);
		initialize();
	}

	private void initialize() {
		sm = new StockManager();
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		{
			jPanel1 = new JPanel();
			GridLayout jPanel1Layout = new GridLayout(1, 1);
			jPanel1Layout.setRows(12);
			jPanel1Layout.setHgap(5);
			jPanel1Layout.setVgap(5);
			jPanel1.setLayout(jPanel1Layout);
			jPanel1.add(TransactionUI.jLabelDate);
			jTextFieldDate = new JFormattedTextField(ClientUtil.getDefaultDateFormatter());
			jTextFieldDate.setValue(new Date());
			jPanel1.add(jTextFieldDate);
			jPanel1.add(TransactionUI.jLabelType);
			jComboBoxType = new JComboBox(TransactionTypes.values);
			jPanel1.add(jComboBoxType);
			jPanel1.add(TransactionUI.jLabelCode);
			stList = sm.getStockList(null);
			codeList = new ArrayList();
			nameList = new ArrayList();
			for (Object obj : stList) {
				Stock st = (Stock) obj;
				codeList.add(st.code);
				nameList.add(st.name);
			}
			jComboBoxCode = new JComboBox(codeList.toArray());
			jPanel1.add(jComboBoxCode);
			jComboBoxCode.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent evt) {
					jComboBoxCodeItemStateChanged(evt);
				}
			});
			jPanel1.add(TransactionUI.jLabelName);
			jTextFieldName = new JTextField();
			jTextFieldName.setText(nameList.get(0).toString());
			jPanel1.add(jTextFieldName);
			jPanel1.add(TransactionUI.jLabelPrice);
			jTextFieldPrice = new JFormattedTextField();
			jTextFieldPrice.setDocument(ClientUtil.getDefaultNumericDocument());
			jPanel1.add(jTextFieldPrice);
			jPanel1.add(TransactionUI.jLabelAmount);
			jTextFieldAmount = new JFormattedTextField();
			jTextFieldAmount.setDocument(ClientUtil.getDefaultNumericDocument());
			jPanel1.add(jTextFieldAmount);
			jPanel1.add(TransactionUI.jLabelSum1);
			jTextFieldSum1 = new JFormattedTextField();
			jTextFieldSum1.setEditable(false);
			jPanel1.add(jTextFieldSum1);
			jPanel1.add(TransactionUI.jLabelShouxufei);
			jTextFieldShouxufei = new JFormattedTextField();
			jTextFieldShouxufei.setDocument(ClientUtil.getDefaultNumericDocument());
			jPanel1.add(jTextFieldShouxufei);
			jPanel1.add(TransactionUI.jLabelYinhuashui);
			jTextFieldYinhuashui = new JFormattedTextField();
			jTextFieldYinhuashui.setDocument(ClientUtil.getDefaultNumericDocument());
			jPanel1.add(jTextFieldYinhuashui);
			jPanel1.add(TransactionUI.jLabelGuohufei);
			jTextFieldGuohufei = new JFormattedTextField();
			jTextFieldGuohufei.setDocument(ClientUtil.getDefaultNumericDocument());
			jPanel1.add(jTextFieldGuohufei);
			jPanel1.add(TransactionUI.jLabelSum2);
			jTextFieldSum2 = new JFormattedTextField();
			jTextFieldSum2.setEditable(false);
			jPanel1.add(jTextFieldSum2);
			jPanel1.add(TransactionUI.jLabelCurrentCash);
			jTextFieldCurrentCash = new JFormattedTextField();
			jPanel1.add(jTextFieldCurrentCash);
			this.add(jPanel1, BorderLayout.CENTER);
		}
		{
			jPanel2 = new JPanel();
			this.add(jPanel2, BorderLayout.SOUTH);
			jButtonOK = new JButton();
			jPanel2.add(jButtonOK);
			jButtonCancel = new JButton();
			jPanel2.add(jButtonCancel);
			jButtonCancel.setText(Constants.label_operationCancel);
			jButtonCancel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent evt) {
					jButtonCancelMouseClicked(evt);
				}
			});
		}
		{
			jPanel3 = new JPanel();
			jPanel3.add(TransactionUI.jLabelThis);
			this.add(jPanel3, BorderLayout.NORTH);
		}
	}

	protected void contructTransaction() {
		if (Util.hasValue(this.jTextFieldDate.getText())) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				tx.date = df.parse(this.jTextFieldDate.getText());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		tx.type = this.jComboBoxType.getSelectedItem().toString();
		tx.code = this.jComboBoxCode.getSelectedItem().toString();
		tx.name = this.jTextFieldName.getText();
		if (Util.hasValue(this.jTextFieldPrice.getText())) {
			tx.price = Float.parseFloat(this.jTextFieldPrice.getText());
		}
		if (Util.hasValue(this.jTextFieldAmount.getText())) {
			tx.amount = Integer.parseInt(this.jTextFieldAmount.getText());
		}
		if (Util.hasValue(this.jTextFieldSum1.getText())) {
			tx.sum1 = Float.parseFloat(this.jTextFieldSum1.getText());
		}
		if (Util.hasValue(this.jTextFieldShouxufei.getText())) {
			tx.shouxufei = Float.parseFloat(this.jTextFieldShouxufei.getText());
		}
		if (Util.hasValue(this.jTextFieldYinhuashui.getText())) {
			tx.yinhuashui = Float.parseFloat(this.jTextFieldYinhuashui.getText());
		}
		if (Util.hasValue(this.jTextFieldGuohufei.getText())) {
			tx.guohufei = Float.parseFloat(this.jTextFieldGuohufei.getText());
		}
	}

	private void jButtonCancelMouseClicked(MouseEvent evt) {
		System.out.println("jButtonCancel.mouseClicked, event=" + evt);
		this.setVisible(false);
	}

	private void jComboBoxCodeItemStateChanged(ItemEvent evt) {
		System.out.println("jComboBoxCode.itemStateChanged, event=" + evt);
		if (this.jComboBoxCode.getSelectedItem() != null) {
			this.jTextFieldName.setText(nameList.get(this.jComboBoxCode.getSelectedIndex())
					.toString());
		}
	}

} // @jve:decl-index=0:visual-constraint="28,-3"
